($ => {
    $.getReportTools = ($eventEmitter, quickFilter, advancedSearch, examGrid, form, toolbars, params) => {
        const handleClick = (toolId, data) => {
            $eventEmitter.trigger(toolId, {
                quickFilter,
                advancedSearch,
                examGrid,
                form,
                params,
                data,
                updateToolbarItems
            });
        };
        const tools = {
            // 书写报告
            write: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_书写报告',
                options: {
                    text: '书写报告',
                    icon: 'edit',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('write');
                    },
                },
            },
            // 查看报告
            view: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_查看报告',
                options: {
                    text: '查看报告',
                    icon: '/icons/antd_file_pdf.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('view');
                    },
                },
            },
            // 打开报告
            open: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_打开报告',
                options: {
                    text: '打开报告',
                    icon: 'folder',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('open');
                    },
                },
            },
            // 保存
            save: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_保存',
                options: {
                    text: '保存',
                    icon: 'save',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('save');
                    },
                },
            },
            // 刷新
            refresh: {
                widget: 'dxButton',
                location: 'after',
                options: {
                    text: '刷新',
                    icon: 'refresh',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('refresh');
                    },
                },
            },
            // 刷新设置
            refreshSetting: {
                widget: 'dxDropDownButton',
                location: 'after',
                options: {
                    text: '刷新设置',
                    icon: 'preferences',
                    focusStateEnabled: false,
                    dropDownOptions: {
                        minWidth: '160px',
                    }
                },
            },
            // 自动刷新
            autoRefresh: {
                widget: 'dxButton',
                location: 'after',
                options: {
                    text: '启用自动刷新',
                    icon: 'refresh',
                    focusStateEnabled: false,
                    onClick: (e) => {
                        handleClick('autoRefresh', { component: e, key: 'reportRefreshInterval', intervalKey: 'reportInterval' });
                    },
                },
            },
            // 刷新间隔时间
            refreshInterval: {
                widget: 'dxButton',
                location: 'after',
                options: {
                    text: '设置间隔时间',
                    icon: 'clock',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('refreshInterval', { key: 'reportRefreshInterval', intervalKey: 'reportInterval' });
                    },
                },
            },
            // 打印正式报告
            formalPrint: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_打印正式报告',
                options: {
                    text: '打印正式报告',
                    icon: 'print',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('print', { complete: true });
                    },
                },
            },
            // 打印临时报告
            temporarilyPrint: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_打印临时报告',
                options: {
                    text: '打印临时报告',
                    icon: 'print',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('temporarilyPrint');
                    },
                },
            },
            // 打印
            print: {
                widget: 'dxDropDownButton',
                location: 'before',
                name: '报告_打印报告',
                options: {
                    text: '打印报告',
                    icon: 'print',
                    focusStateEnabled: false,
                    dropDownOptions: {
                        minWidth: '150px',
                    }
                },
            },
            printReport: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_打印报告',
                options: {
                    text: '打印报告',
                    icon: 'print',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('print', { complete: true });
                    },
                },
            },
            // 打印正式报告
            printReport2: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_打印正式报告',
                options: {
                    text: '打印报告',
                    icon: 'print',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('print', { complete: true });
                    },
                },
            },
            // 提交报告
            submit: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_提交报告',
                options: {
                    text: '提交报告',
                    icon: '/icons/fa_check.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('submit');
                    },
                },
            },
            // 审核报告
            audit: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_审核报告',
                options: {
                    text: '审核报告',
                    icon: '/icons/antd_audit.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('audit');
                    },
                },
            },
            // 标记随访
            markFollowUp: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_标记随访',
                options: {
                    text: '标记随访',
                    icon: '/icons/antd_pushpin.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('markFollowUp');
                    },
                },
            },
            // 随访记录
            followUpRecord: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_随访记录',
                options: {
                    text: '随访记录',
                    icon: '/icons/antd_snippets.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('followUpRecord');
                    },
                },
            },
            // 患者回访
            revisit: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_患者回访',
                options: {
                    text: '患者回访',
                    icon: '/icons/antd_phone.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('revisit');
                    },
                },
            },
            // 查看申请单
            eApply: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_电子申请单',
                options: {
                    text: '电子申请单',
                    icon: '/icons/antd_filesearch.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('eApply');
                    },
                },
            },
            // 电子病历
            eMedical: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_电子病历',
                options: {
                    text: '电子病历',
                    icon: '/icons/md_content_paste_search.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('eMedical');
                    },
                },
            },
            // 危急值
            criticalValue: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_危急值',
                options: {
                    text: '危急值',
                    icon: '/icons/fa_heart_crack.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('criticalValue');
                    },
                },
            },
            collect: {
                widget: 'dxDropDownButton',
                location: 'before',
                name: '报告_收藏',
                options: {
                    text: '收藏',
                    icon: 'like',
                    focusStateEnabled: false,
                    dropDownOptions: {
                        minWidth: '150px',
                    }
                },
            },
            // 收藏
            collectTo: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_收藏',
                options: {
                    text: '收藏检查',
                    icon: '/icons/antd_star_fill.svg',
                    onClick: (e) => {
                        handleClick('collectTo');
                    },
                },
            },
            // 取消收藏
            cancelCollect: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_取消收藏',
                options: {
                    text: '取消收藏',
                    icon: '/icons/antd_star.svg',
                    onClick: (e) => {
                        handleClick('cancelCollect');
                    },
                },
            },
            // 撤回
            revoke: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_撤回',
                options: {
                    text: '撤回',
                    icon: 'arrowback',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('revoke');
                    },
                },
            },
            // 报告页面，队列列表工具栏
            // 呼叫下一位
            callNext: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_队列_呼叫下一位',
                options: {
                    text: '呼叫下一位',
                    icon: '/icons/ionic_volume_medium.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('callNext');
                    },
                }
            },
            // 呼叫患者（具体患者）
            call: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_队列_呼叫患者',
                options: {
                    text: '呼叫患者',
                    icon: '/icons/ionic_volume_medium.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('call');
                    },
                }
            },
            // 挂起
            suspend: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_队列_挂起',
                options: {
                    text: '挂起检查',
                    icon: '/icons/md_pause_circle_outline.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('suspend');
                    },
                },
            },
            // 过号
            skip: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_队列_过号',
                options: {
                    text: '过号检查',
                    icon: '/icons/antd_redo.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('skip');
                    },
                },
            },
            // 报告查询
            reportQuickFilter: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_快速查询',
                options: {
                    text: '查询',
                    icon: 'search',
                    focusStateEnabled: false,
                    onClick: (e) => {
                        handleClick('reportQuickFilter', { ele: e.element });
                    },
                }
            },
            // 队列查询
            technicianQuickFilter: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_队列_快速查询',
                options: {
                    text: '查询',
                    icon: 'search',
                    focusStateEnabled: false,
                    onClick: (e) => {
                        handleClick('technicianQuickFilter', { ele: e.element });
                    },
                }
            },
            // 查看图像
            viewImage: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_查看图像',
                options: {
                    text: '查看图像',
                    icon: '/icons/antd_image.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('viewImage');
                    },
                },
            },
            //  通过审核
            complete: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_通过审核',
                options: {
                    text: '通过审核',
                    icon: '/icons/fa_check.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('submit', { complete: true });
                    },
                },
            },
            //  申请复审
            applyReview: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_申请复审',
                options: {
                    text: '申请复审',
                    icon: 'arrowup',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('submit');
                    },
                },
            },
            //  修正
            amend: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_修正报告',
                options: {
                    text: '修正报告',
                    icon: 'edit',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('amend');
                    },
                },
            },
            //  驳回重写
            reject: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_驳回重写',
                options: {
                    text: '驳回重写',
                    icon: 'edit',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('reject');
                    },
                },
            },
            //  切换报告样式
            reportStyle: {
                location: 'before',
                template: '<font style="margin-left: 15px;">报告样式:</font>'
            },
            changeReportStyle: {
                widget: 'dxDropDownButton',
                location: 'before',
                name: '报告_切换报告样式',
                options: {
                    text: '切换报告样式',
                    icon: '报告样式',
                    focusStateEnabled: false,
                    displayExpr: 'name',
                    keyExpr: 'id',
                    dropDownOptions: {
                        minWidth: '160px'
                    },
                    useSelectMode: true,
                    onItemClick(e) {
                        handleClick('changeReportStyle', e.itemData);
                    }
                },
                visible: true
            },
            // 显示视频窗口
            showVideoWindow: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_显示视频窗口',
                visible: !!ris.host,
                options: {
                    text: '显示视频窗口',
                    icon: '/icons/antd_video.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('showVideoWindow');
                    },
                },
            },
            // 设为采集状态
            setCapturingStatus: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_设为采集状态',
                visible: !!ris.host,
                options: {
                    text: '设为采集状态',
                    icon: '/icons/fa_check.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('setCapturingStatus');
                    },
                },
            },
            addRemark: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_添加备注',
                options: {
                    text: '添加',
                    icon: 'add',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('addRemark');
                    },
                },
            },
            deleteRemark: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_删除备注',
                options: {
                    text: '删除',
                    icon: 'trash',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('deleteRemark');
                    },
                },
            },
            updateRemark: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_修改备注',
                options: {
                    text: '修改',
                    icon: 'edit',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('updateRemark');
                    },
                },
            },
            // 开始检查
            start: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_队列_开始检查',
                options: {
                    text: '开始检查',
                    icon: '/icons/antd_play_circle.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('start');
                    },
                }
            },
            // 放弃书写
            giveUpWrite: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_放弃书写',
                options: {
                    text: '放弃书写',
                    icon: '/icons/md_undo.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('giveUpWrite');
                    },
                }
            },
            // 移交
            transfer: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_移交报告',
                options: {
                    text: '移交报告',
                    icon: '/icons/md_swap_horiz.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('transfer');
                    },
                },
                visible: true
            },
            // 删除
            delete: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_删除检查',
                options: {
                    text: '删除检查',
                    icon: 'trash',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('delete');
                    },
                },
            },
            // 申请会诊
            applyConsultation: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_申请会诊',
                options: {
                    text: '申请会诊',
                    icon: '/icons/x_exam.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('applyConsultation');
                    },
                },
            },
            // 拆分报告
            disassemble: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_拆分报告',
                options: {
                    text: '拆分报告',
                    icon: '/icons/antd_solit_cells.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('disassemble');
                    },
                }
            },
            queueStatus: {
                location: 'before',
                template: '<div style="margin-left:5px" />'
            },
            // 等候的
            wait: {
                widget: 'dxButton',
                location: 'before',
                options: {
                    stylingMode: 'outlined',
                    text: '等候检查',
                    focusStateEnabled: false,
                    elementAttr: {
                        class: 'report-detail-queuing-filter-button',
                        style: 'background-color:#00000010'
                    },
                    onClick: (e) => {
                        updateQueuingFilterButton(e.component);
                        handleClick('searchQueue', { status: 'wait' });
                    },
                }
            },
            // 准备的
            ready: {
                widget: 'dxButton',
                location: 'before',
                options: {
                    stylingMode: 'outlined',
                    text: '准备检查',
                    focusStateEnabled: false,
                    elementAttr: {
                        class: 'report-detail-queuing-filter-button'
                    },
                    onClick: (e) => {
                        updateQueuingFilterButton(e.component);
                        handleClick('searchQueue', { status: 'ready' });
                    },
                }
            },
            // 过号的
            skiped: {
                widget: 'dxButton',
                location: 'before',
                options: {
                    stylingMode: 'outlined',
                    text: '过号检查',
                    focusStateEnabled: false,
                    elementAttr: {
                        class: 'report-detail-queuing-filter-button'
                    },
                    onClick: (e) => {
                        updateQueuingFilterButton(e.component);
                        handleClick('searchQueue', { status: 'skiped' });
                    },
                }
            },
            // 挂起的
            pending: {
                widget: 'dxButton',
                location: 'before',
                options: {
                    stylingMode: 'outlined',
                    text: '挂起检查',
                    focusStateEnabled: false,
                    elementAttr: {
                        class: 'report-detail-queuing-filter-button'
                    },
                    onClick: (e) => {
                        updateQueuingFilterButton(e.component);
                        handleClick('searchQueue', { status: 'pending' });
                    },
                }
            },
            // 完成的
            completed: {
                widget: 'dxButton',
                location: 'before',
                options: {
                    stylingMode: 'outlined',
                    text: '完成检查',
                    focusStateEnabled: false,
                    elementAttr: {
                        class: 'report-detail-queuing-filter-button'
                    },
                    onClick: (e) => {
                        updateQueuingFilterButton(e.component);
                        handleClick('searchQueue', { status: 'completed' });
                    },
                }
            },
            // 查看检查事件
            examEvent: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_检查事件',
                options: {
                    text: '检查事件',
                    icon: '/icons/antd_thunderbolt.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('examEvent');
                    },
                }
            },
            // 导出图像
            outputImage: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_导出图像',
                options: {
                    text: '导出图像',
                    icon: '/icons/antd_cloud_download.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('outputImage');
                    },
                }
            },
            // 转发图像
            relayImage: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_转发图像',
                options: {
                    text: '转发图像',
                    icon: '/icons/antd_send.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('relayImage');
                    },
                }
            },
            export: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_导出列表',
                options: {
                    text: '导出列表',
                    icon: 'export',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('export');
                    },
                }
            },
            // 重制报告
            remake: {
                widget: 'dxButton',
                location: 'before',
                name: '报告_重新生成报告',
                options: {
                    text: '重制报告',
                    icon: '/icons/antd_filesync.svg',
                    focusStateEnabled: false,
                    onClick: () => {
                        handleClick('remake');
                    },
                }
            }
        };

        for (const property in tools) {
            const item = tools[property];
            if (item.name) {
                item.visible = item.visible || (ris.verify(item.name) && item.visible !== false);
            }
            if (property === 'print') {
                item.visible = tools['formalPrint'].visible && tools['temporarilyPrint'].visible;
            }
            if (property === 'printReport2') {
                item.visible = !tools['temporarilyPrint'].visible && item.visible;
            }
            if (property === 'write') {
                item.visible = ris.verify(item.name) || ris.verify('报告_初写报告');
            }
            if (property === 'complete') {
                item.visible = ris.verify(item.name);
            }
            if (property === 'audit') {
                item.visible = ris.verify(item.name) || ris.verify('报告_复审报告');
            }
            if (item.options) {
                if (item.hasOwnProperty('visible')) {
                    item.options.visible = item.visible;
                }
            }
        }

        const updateQueuingFilterButton = button => {
            $('.report-detail-queuing-filter-button').each((_, item) => {
                $(item).css('background-color', 'unset');
                $(item).css('background-color', '');
            });
            button.element().css('background-color', '#00000010');
        };

        const updateToolbarItems = (report) => {
            const reportStatus = report.reportStatus;
            const permissions = report.permissions;
            {
                for (var i = 0; i < toolbars.length; i++) {
                    const toolbar = toolbars[i];
                    const items = toolbar.option('items');
                    if (items.length > 0) {
                        items.forEach(e => {
                            if (["报告_保存", "报告_提交报告", "报告_修正报告", "报告_书写报告", "报告_审核报告", "报告_撤回",
                                "报告_通过审核", "报告_申请复审", "报告_驳回重写", "报告_切换报告样式", "报告_放弃书写"].includes(e.name)) {
                                e.disabled = true;
                                if (e.name != '报告_切换报告样式') {
                                    e.visible = false;
                                }
                            }
                            if (permissions.includes(e.name)) {
                                e.disabled = false;
                                e.visible = ris.verify(e.name);
                            }
                            if (e.name == '报告_切换报告样式') {
                                e.options.text = report.reportStyle?.displayName;
                            }
                            if (e.name === "报告_撤回" && permissions.includes(e.name)) {
                                e.disabled = false;
                                e.visible = ris.verify(e.name);
                                switch (reportStatus) {
                                    case 1:
                                        e.options.text = "撤回提交";
                                        break;
                                    case 3:
                                        //已提交报告  撤回提交
                                        e.options.text = "撤回提交";
                                        break;
                                    case 5:
                                        //已审核  撤回审核 / 申请复审（提交并且复审）
                                        e.options.text = "撤回审核";
                                        break;
                                    case 8:
                                        //已完成  撤回 / 复审报告（权限允许自审）
                                        e.options.text = "撤回审核";
                                        break;
                                }
                            }
                            if (report.isEditing) {
                                if (e.name === "报告_保存" && permissions.includes(e.name)) {
                                    e.disabled = false;
                                    e.visible = ris.verify(e.name);
                                }
                                if (e.name === "报告_提交报告" && permissions.includes(e.name)) {
                                    e.disabled = false;
                                    e.visible = ris.verify(e.name);
                                }
                                if (e.name === "报告_修正报告") {
                                    e.disabled = true;
                                    e.visible = false;
                                }
                            }
                            if (e.name === '报告_标记随访') {
                                if (!report.isFollowup) {
                                    e.options.text = '标记随访';
                                    e.options.icon = '/icons/antd_pushpin.svg';
                                } else {
                                    e.options.text = '放弃随访';
                                    e.options.icon = '/icons/antd_pushpin.svg';
                                }
                            }

                            if (report.readOnly && e.name === '报告_标记随访') {
                                e.visible = false;
                                e.disabled = true;
                            }
                        });
                    }
                    toolbar.option('items', items);
                }
            };
        }
        return { ...tools, updateToolbarItems };
    };
})(jQuery);
